package fr.umlv.galcon.utils;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

import fr.umlv.galcon.game.GalconIG;

public class Utils {
    public static final int FRAME_WIDTH = 900;
    public static final int FRAME_HEIGHT = 600;

    public static final int NB_HUMAN_PLAYER = 1;
    public static final int NB_CPU_PLAYER = 3;

    public static final int PLANET_SIZE = 100;

    public static final int NEUTRAL_SHIP = 50;
    public static final int PLAYER_SHIP = 200;
    public static final int INFO_AREA = 150;

    private static final int PLAYER1 = 1;
    private static final int PLAYER2 = 2;
    private static final int PLAYER3 = 3;
    private static final int PLAYER4 = 4;
    private static final int NEUTRALPLAYER = 0;

    private static final HashMap<Integer, Color> colorMap = new HashMap<Integer, Color>();

    private static BufferedImage explosionPicture = null;
    private static BufferedImage winnerPicture = null;

    private static final String picturesPath = "pictures/";

    private static final String explosionFile = picturesPath + "explosion.png";
    private static final String winnerFile = picturesPath + "winner.jpg";

    /**
     * Get the explosion file to draw when the ship is destroy
     * 
     * @return the explosion file
     * @throws IOException
     */
    public static BufferedImage getExplosionFile() throws IOException {
	if (explosionPicture == null) {
	    URL url = GalconIG.class.getResource(explosionFile);
	    if (url != null)
		explosionPicture = ImageIO.read(url);
	    else
		explosionPicture = ImageIO.read(new File(explosionFile));
	    return explosionPicture;
	} else {
	    return explosionPicture;
	}
    }

    /**
     * Get the winner file to draw when the game is over
     * 
     * @return the winner file
     * @throws IOException
     */
    public static BufferedImage getWinnerFile() throws IOException {
	if (winnerPicture == null) {
	    URL url = GalconIG.class.getResource(winnerFile);
	    if (url != null)
		winnerPicture = ImageIO.read(url);
	    else
		winnerPicture = ImageIO.read(new File(winnerFile));
	    return winnerPicture;
	} else {
	    return winnerPicture;
	}
    }

    /**
     * Get the color of a player.
     * 
     * @param idPlayer
     *            The "id" of the player.
     * @return The Color of the player.
     */
    public static Color getPlayerColor(int idPlayer) {
	if (colorMap.isEmpty()) {
	    colorMap.put(PLAYER1, Color.BLUE);
	    colorMap.put(PLAYER2, Color.GREEN);
	    colorMap.put(PLAYER3, Color.RED);
	    colorMap.put(PLAYER4, Color.YELLOW);
	    colorMap.put(NEUTRALPLAYER, Color.GRAY);
	}
	return colorMap.get(idPlayer);
    }
}
